//
//  LMCellView.m
//  Perking
//
//  Created by longmin on 16/7/15.
//  Copyright © 2016年 tim. All rights reserved.
//

#import "LMCellView.h"

@interface LMCellView()<UITextFieldDelegate>

@property(nonatomic, strong) UIView *bottomLine;

@property(nonatomic, strong) UIView *topLine;

@end

@implementation LMCellView

#pragma mark - 生命周期

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.control];
        [self addSubview:self.imageView];
        [self addSubview:self.textLabel];
        [self addSubview:self.detailFied];
        [self addSubview:self.accessoryView];
        [self addSubview:self.bottomLine];
        [self addSubview:self.topLine];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
#pragma mark - set方法和get方法

-(UIControl *)control {
    if (_control == nil) {
        _control = [[UIControl alloc] init];
        [self.control addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
        _control.lm_repeatInterval = 1.0;
    }
    return _control;
}

-(UIImageView *)imageView {
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}

-(UILabel *)textLabel{
    if (_textLabel == nil) {
        _textLabel = [[UILabel alloc] init];
        _textLabel.font = _kFont32;
        _textLabel.textColor = kColor3C3C3C;
    }
    return _textLabel;
}

-(UITextField *)detailFied{
    if (_detailFied == nil) {
        _detailFied = [[UITextField alloc] init];
        _detailFied.font = _kFont32;
        _detailFied.textColor = kColorA6A6A6;
        _detailFied.delegate = self;
        _detailFied.returnKeyType = UIReturnKeyDone;
        _detailFied.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    }
    return _detailFied;
}

-(UIImageView *)accessoryView{
    if (_accessoryView == nil) {
        _accessoryView = [[UIImageView alloc] init];
    }
    return _accessoryView;
}

-(UIView *)bottomLine{
    if (_bottomLine == nil) {
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = kColorD9D9D9;
    }
    return _bottomLine;
}

-(UIView *)topLine{
    if (_topLine == nil) {
        _topLine = [[UIView alloc] init];
        _topLine.backgroundColor = self.bottomLine.backgroundColor;
    }
    return _topLine;
}

#pragma mark - IBActions

/** 点击事件 */
-(void)controlAction:(UIControl *)control{
    
    if (self.clickBlock) {
        self.clickBlock();
        
        control.backgroundColor = kColorD9D9D9;
        [UIView animateWithDuration:0.618 animations:^{
            control.backgroundColor = [UIColor clearColor];
        }];

    }
}
#pragma mark - Public

+(instancetype)circleCellViewWithHeight:(CGFloat)height clickHandler:(void (^)())clickBlock {
    
    CGFloat cellW = [UIScreen mainScreen].bounds.size.width - 24;
    
    LMCellView *cellView = [[self alloc] initWithFrame:CGRectMake(0, 0, cellW, height)];
    
    cellView.centerX = __kScreenWidth * 0.5;
    
    cellView.layer.cornerRadius = 8.0;
    cellView.clipsToBounds = YES;
    
    cellView.clickBlock = clickBlock;
    [cellView removeLine];
    
    return cellView;
}

+ (instancetype)cellViewWithHeight:(CGFloat)height
{
    LMCellView *cellView = [[self alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height)];
    cellView.textLeft = MK_PADDING_30;
    cellView.detailFied.textAlignment = NSTextAlignmentLeft;
    cellView.textLabel.font = _kFont32;
    cellView.textLabel.textColor = kColor3C3C3C;
    cellView.detailFied.textColor = kColorA6A6A6;
    cellView.detailFied.font = _kFontMain;
    cellView.detailLeft = 80;
    return cellView;
    
}

+ (instancetype)cellViewWithNoActionHeight:(CGFloat)height  {
    LMCellView *cellView = [self cellViewWithHeight:height];
    cellView.detailFied.userInteractionEnabled = NO;
//    cellView.detailFied.textColor = kColorD9D9D9;
    return cellView;
}

+(instancetype)cellViewWithHeight:(CGFloat)height clickHandler:(void (^)())clickBlock {
    LMCellView *cellView = [self cellViewWithHeight:height];
    cellView.clickBlock = clickBlock;
    cellView.detailFied.userInteractionEnabled = NO;
    return cellView;
}

-(void)setPlaceHolder:(NSString *)placeHolder attribute:(NSDictionary *)attribute {
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:placeHolder attributes:attribute];
    self.detailFied.attributedPlaceholder = attrStr;
}

-(void)removeLine {
    [self.topLine removeFromSuperview];
    [self.bottomLine removeFromSuperview];
}

-(void)removeTopLine {
    [self.topLine removeFromSuperview];
}

-(void)removeBottomLine {
    [self.bottomLine removeFromSuperview];
}

#pragma mark - Private
#pragma mark - 数据源和代理方法
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSString *resultStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if ([string isEqualToString:@""]) {
        return YES;
    }
    
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    
    if ([string isEqualToString:@" "]) {
        return NO;
    }
    
    
    if (self.maxDetailLength) {
        if (range.location + range.length >= self.maxDetailLength) {
            NSString *tipStr = [NSString stringWithFormat:@"不能超过%zd个字",self.maxDetailLength];
            [MBProgressHUD showText:tipStr onView:nil];
            return NO;
        }
    }
    
    if (self.isOnlyAllowNum) {
        return [string mj_isPureInt];
    }
    
    if (self.regular.length) {
        return [CommonTool isValidateRegularExpression:resultStr byExpression:self.regular];
    }
    
    return YES;
}
#pragma mark - UIView -> 下面放继承自UIView的方法

-(BOOL)resignFirstResponder {
    [super resignFirstResponder];
    
    [self.detailFied resignFirstResponder];
    
    return YES;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.control.frame = self.bounds;
    
    CGFloat textLeft = self.textLeft ? self.textLeft : 20;
    if (self.imageView.image) {
        CGFloat imageW = self.imageW ? self.imageW : 35;
        self.imageView.width = self.imageView.height = imageW;
        self.imageView.left = 18;
        self.imageView.centerY = CGRectGetCenter(self.bounds).y;
        textLeft = self.imageView.right + 5;
    }
    
    
    //textLabel
    CGSize textSize = [self.textLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.textLabel.font.lineHeight)
                                                        options:NSStringDrawingUsesLineFragmentOrigin
                                                     attributes:@{NSFontAttributeName:self.textLabel.font}
                                                        context:nil].size;
    self.textLabel.frame = CGRectMake(textLeft, (self.bounds.size.height-textSize.height) * 0.5-1, textSize.width, textSize.height);
    
    //accessoryView
    if (!self.accessoryViewSize) {
        self.accessoryViewSize = 12;
    }
    CGFloat accessoryW = self.accessoryViewSize;
    CGFloat accessoryH = self.accessoryViewSize;
    CGFloat rightPadding = 15;
    
    if (self.accessoryView.image) {
        
        self.accessoryView.frame = CGRectMake(self.width - accessoryW - rightPadding,
                                              (self.bounds.size.height-accessoryH) * 0.5,
                                              accessoryW,
                                              accessoryH);
    }else{
        accessoryW = 0;
        rightPadding = 0;
    }
    
    
    //deatilFied
    CGFloat detailLeft = self.detailLeft ? self.detailLeft : self.textLabel.right + 20;
    if (self.spaceBetweenDetailAndText != 0.0) {
        detailLeft = self.textLabel.right + self.spaceBetweenDetailAndText;
    }
    
    CGFloat detailH = self.height;
    self.detailFied.frame = CGRectMake(detailLeft ,
                                       (self.height - detailH) * 0.5,
                                       self.width - detailLeft - accessoryW - rightPadding - 15,
                                       detailH);
    //底部灰线
    self.bottomLine.frame = CGRectMake(MK_PADDING_30, self.bounds.size.height-0.5, self.bounds.size.width-MK_PADDING_30, 0.5);
    //顶部灰线
    self.topLine.frame = CGRectMake(MK_PADDING_30, 0, self.bounds.size.width-MK_PADDING_30, 0.5);
    
    if (!self.accessoryView.image) {
        self.accessoryView.hidden = YES;
    }else{
        self.accessoryView.hidden = NO;
    }
}

@end
